package com.tianyang.yukuaifu.base;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.module.dialog.RemindDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.TwoBtnClickListener;
import com.tianyang.yukuaifu.utils.IntentTool;
import com.tianyang.yukuaifu.utils.annotation.ModuleAnnotation;
import com.tianyang.yukuaifu.utils.permissions.PermissionsConstant;

/**
 * Description：公共跳转功能
 */
@ModuleAnnotation(module = "ykfprovider")
public class CommonProvider implements ICommonProvider {

    @Override
    public void openAppPermission(final Activity activity, final String permission, boolean isFinish) {
        if (activity != null && !activity.isFinishing()) {
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(activity.getResources().getString(R.string.permission_prompt_start));
            switch (permission) {
                case PermissionsConstant.WRITE_STORAGE:
                case PermissionsConstant.READ_STORAGE:
                    stringBuffer.append("存储空间");
                    break;
                case PermissionsConstant.READ_PHONE_STATE:
                    stringBuffer.append("电话");
                    break;
                case PermissionsConstant.CAMERA:
                    stringBuffer.append("相机");
                    break;
                case PermissionsConstant.FINE_LOCATION:
                case Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS:
                    stringBuffer.append("位置信息");
                    break;
                default:
                    return;
            }
            stringBuffer.append(activity.getResources().getString(R.string.permission_prompt_end));
            new RemindDialog(activity).title(activity.getResources().getString(R.string.permission_title))
                    .content(stringBuffer.toString())
                    .cancel(activity.getResources().getString(R.string.cancel))
                    .comfirm(activity.getResources().getString(R.string.permission_setting))
                    .cancelable(false)
                    .listener(new TwoBtnClickListener() {
                        @Override
                        public void onLeftClick() {
                            if (isFinish)
                                activity.finish();
                        }

                        @Override
                        public void onRightClick() {
                            openSystemSetting(activity);
                        }
                    })
                    .show();
        }
    }

    @Override
    public void exitApp(Activity activity) {
        int currentVersion = android.os.Build.VERSION.SDK_INT;
        if (currentVersion > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(startMain);
            System.exit(0);
        } else {// android2.1
            ActivityManager am = (ActivityManager) activity
                    .getSystemService(Context.ACTIVITY_SERVICE);
            am.restartPackage(activity.getPackageName());
        }
    }

    @Override
    public void openSystemSetting(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.setting.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        IntentTool.startActivity(context, localIntent);
    }
}
